<?php
//error_reporting(0);
header("Content-type: text/html; charset=utf-8");

require_once './phpqrcode.php';
//require_once './GoogleAuthenticator.php';
//$ga = new PHPGangsta_GoogleAuthenticator();
//$secret = 'Z5XFVH357RVHU26Z'   ;//$ga->createSecret();
////$qrCodeUrl = $ga->getQRCodeGoogleUrl('872871448', $secret, 'kuaxue');
//$checkResult = $ga->verifyCode($secret, '991338', 1);
//echo $checkResult;exit;


//for ($i=0;$i<=100;$i++){
//    $e=file_get_contents('https://pay.bbnon.com');
//    echo '<br>';
//    echo $e;
//    exit();
//}


$data = $_POST;
foreach ($data as $k => $v) {
    if (empty($v)) {
        die("{$k}参数信息不完整");
    }
}
//alipay.shuzi88.com
$apiurl = "http://merchants.yjpaa.com/pay-pay.htm";//接入URL
$data = [
    'partner' => $data['partner'],//商户ID
    'banktype' => 'ALIPAY',//支付类型
    'paymoney' => $data['paymoney'],//金额（单位:元）
    'ordernumber' => $data['ordernumber'],//商户订单号（该值需在商户系统内唯一）
    'subject' => $data['subject'],//订单标题 原值返回
    'callbackurl' => $data['callbackurl'],//异步通知地址
    'hrefbackurl' => $data['hrefbackurl'],//同步地址
    'chname' => '',//指定通道名称默认为空
    'key' => '8bd557b353ebe27b237a79550e548709'//密钥
];

$data['sign'] = sign($data);
unset($data['key']);
$res = httpPost($apiurl, $data);
$result = json_decode($res,true);

if (is_null($result)) {
    var_dump($res);exit();
}else{
    $url = true;
    $msg='';
    $isMobile = isMobile();
    if ($result['status'] == 10000 && isset($result['status'])) {
        if ($isMobile) {
            $url = $result['url'];
        }else{
            $file ="qrcode/{$data['ordernumber']}.png";
            QRcode::png($result['url'], "$file",'M',6,2);
            echo "<div style='text-align: center'><img src='$file'</div>";
            exit();
        }

    } else {
        $msg = $res;
    }
}

// 签名函数
function sign($data)
{
    ksort($data);
    $sign = [];
    foreach ($data as $k => $v) {
        if ($k != 'sign') {
            $sign[] = $k . '=' . $v;
        }
    }
    return md5(implode('&', $sign));
}


function isMobile() {
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset($_SERVER['HTTP_VIA'])) {
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger');
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
}


function httpPost($url, $data)
{
    //初始化
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出 关闭请求头
    curl_setopt($curl, CURLOPT_HEADER, false);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    // 取消 ssl 认证 不取消 获取不到返回值
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    //设为0表示不检查证书设为1表示检查证书中是否有CN(common name)字段
//    设为2表示在1的基础上校验当前的域名是否与CN匹配
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    //执行命令
    $data = curl_exec($curl);
    $a = curl_error($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    return $data;
}

?>
<div id="img">  </div>

<script src="http://mat1.gtimg.com/libs/jquery2/2.2.0/jquery.js"></script>
<script>
    $(function () {
       var url ="<?php echo $url ?>";
       var ism ="<?php echo $isMobile ?>";
        if (url) {
            if (ism) {
                window.location.href = url
            }
        } else {
            alert("<?php echo $msg ?>");
        }
    })
</script>

